package com.info.zhiduoduo.repository.mapper.maker;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.maker.MakerRescissionApplyDto;
import com.info.zhiduoduo.repository.entity.maker.MakerRescissionApply;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author bob
 * @since 2020-02-07
 */
public interface MakerRescissionApplyMapper extends BaseMapper<MakerRescissionApply> {

    @Select(" select * from zdd_maker_rescission_apply mra " +
            " where userid = #{userid} " +
            " order by id desc " +
            " limit 1 ")
    MakerRescissionApply getByUserid(int userid);

    @Select(" select mra.*,m.bindcompid,co.intel_crowdsource, ma.realname,ms.username,m.phone,co.compname," +
            " CASE WHEN mal.userid > 0 THEN 1 ELSE 0 END as isOpenAlipay," +
            " CASE WHEN ub2.acctno is null THEN 0 ELSE 1 END as isOpenAccNo2 " +
            " from zdd_maker_rescission_apply mra " +
            " join zdd_maker m on m.userid = mra.userid " +
            " join zdd_maker_account ma on ma.userid = mra.userid " +
            " join zdd_maker_studio ms on ms.userid = mra.userid " +
            " join zdd_company co on co.compid = m.bindcompid" +
            " left join zdd_maker_alipay mal on mal.userid = mra.userid and mal.deleted=0" +
            " left join zdd_maker_bankcard ub2 on ub2.userid = mra.userid and ub2.state = 1 and ub2.accttype = 2" +
            " where CONCAT(ma.realname,ms.username) like '%${name}%' " +
            " and if(#{states}='0', 1=1, mra.state in(${states}))" +
            " ${dockingCity} " +
            " order by mra.id desc ")
    List<MakerRescissionApplyDto> getApplyList(@Param("name") String name, @Param("states") String states, @Param("dockingCity")String dockingCity);

    @Select("SELECT count(ra.id)   \n" +
            "            FROM zdd_maker_rescission_apply ra  \n" +
            "             join zdd_maker m on m.userid = ra.userid  \n" +
            "             join zdd_maker_account ma on ma.userid = ra.userid  \n" +
            "             join zdd_maker_studio ms on ms.userid = ra.userid  \n" +
            "             join zdd_company co on co.compid = m.bindcompid \n" +
            "            WHERE ra.state = #{state}  \n" +
            "            AND IF(ISNULL(#{dockingCity}) OR #{dockingCity}= '',1=1,ma.opening_location in(#{dockingCity})) ")
    int countCancellation(@Param("state") int state,@Param("dockingCity") String dockingCity);
}
